package com.ah.shanxi_demo.common;

import org.apache.commons.lang3.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author Tao on 2019/9/12
 */
public class DateUtils {

    /**
     * 两个时间之差
     *
     * @param startDate
     * @param endDate
     * @return 分钟
     */
    public static Integer getBetweenMinutes(Date startDate, Date endDate) {
        Integer minutes = 0;
        try {
            if (startDate != null && endDate != null) {
                long ss = 0;
                if (startDate.before(endDate)) {
                    ss = endDate.getTime() - startDate.getTime();
                } else {
                    ss = startDate.getTime() - endDate.getTime();
                }
                minutes = Integer.valueOf((int) (ss / (60 * 1000)));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return minutes;
    }

    /**
     * 两个时间只差
     *
     * @param startDate
     * @param endDate
     * @return 分钟
     */
    public static Integer getBetweenMinutes(String a, String b) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date startDate;
        Date endDate;
        try {
            startDate = format.parse(a);
            endDate = format.parse(b);
            try {
                long ss = 0;
                if (startDate.before(endDate)) {
                    ss = endDate.getTime() - startDate.getTime();
                } else {
                    ss = startDate.getTime() - endDate.getTime();
                }
                return Integer.valueOf((int) (ss / (60 * 1000)));
            } catch (Exception e) {
                e.printStackTrace();
                return 0;
            }
        } catch (ParseException e1) {
            e1.printStackTrace();
            return 0;
        }
    }

    /**
     * 两个时间只差
     *
     * @param startDate
     * @param endDate
     * @return 秒数
     */
    public static Integer getBetweenSecond(Date startDate, Date endDate) {
        Integer seconds = 0;
        try {
            if (startDate != null && endDate != null) {
                long ss = 0;
                if (startDate.before(endDate)) {
                    ss = endDate.getTime() - startDate.getTime();
                } else {
                    ss = startDate.getTime() - endDate.getTime();
                }
                seconds = Integer.valueOf((int) (ss / (1000)));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return seconds;
    }

    /**
     * 两个时间只差
     *
     * @param startDate
     * @param endDate
     * @return 秒数
     */
    public static Integer getBetweenSecond(String a, String b) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date startDate;
        Date endDate;
        try {
            startDate = format.parse(a);
            endDate = format.parse(b);
            try {
                long ss = 0;
                if (startDate.before(endDate)) {
                    ss = endDate.getTime() - startDate.getTime();
                } else {
                    ss = startDate.getTime() - endDate.getTime();
                }
                return Integer.valueOf((int) (ss / (1000)));
            } catch (Exception e) {
                e.printStackTrace();
                return 0;
            }
        } catch (ParseException e1) {
            e1.printStackTrace();
            return 0;
        }
    }

    public static String getDatFormat(Date date, String dateFormat) {
        try {
            SimpleDateFormat format = new SimpleDateFormat(dateFormat);
            return format.format(date);
        } catch (Exception e) {
            e.printStackTrace();
            SimpleDateFormat format = new SimpleDateFormat("YYYY-MM-DD");
            return format.format(new Date());
        }
    }

    /**
     * 获取两个时间之间的日期、月份、年份
     *
     * @param beginDate
     * @param endDate
     * @param type{1：日期，2：月份，其他：年份}
     * @return
     */
    public static List<String> getBetweenDates(String startDate, String endDate, Integer type) {
        SimpleDateFormat format = null;
        Date begin = null;
        Date end = null;
        Integer obj = null;
        Integer flag = null;
        if (type != null && type == 1) {
            format = new SimpleDateFormat("yyyy-MM-dd");
            obj = Calendar.DAY_OF_YEAR;
            flag = 9;
        } else if (type != null && type == 2) {
            format = new SimpleDateFormat("yyyy-MM");
            obj = Calendar.MONTH;
            flag = 11;
        } else {
            format = new SimpleDateFormat("yyyy");
            obj = Calendar.YEAR;
            flag = 9;
        }
        if (StringUtils.isNotEmpty(startDate) && StringUtils.isNotEmpty(endDate)) {
            try {
                begin = format.parse(startDate);
                end = format.parse(endDate);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        } else {
            end = new Date();
            begin = getDateBefore(end, flag, obj);
        }

        List<String> result = new ArrayList<String>();
        Calendar tempStart = Calendar.getInstance();
        tempStart.setTime(begin);
        while (begin.getTime() <= end.getTime()) {
            result.add(format.format(tempStart.getTime()));
            tempStart.add(obj, 1);
            begin = tempStart.getTime();
        }
        return result;
    }

    /**
     * 获取两个时间之间的日期、月份、年份
     *
     * @param date{YYYY-MM-DD-YYYY-MM-DD}
     * @param type{1：日期，2：月份，其他：年份}
     * @return
     */
    public static List<String> getBetweenDates(String date, Integer type) {
        SimpleDateFormat format = null;
        Date begin = null;
        Date end = null;
        Integer obj = null;
        String startDate = null;
        String endDate = null;
        Integer flag = null;
        if (type != null && type == 1) {
            format = new SimpleDateFormat("yyyy-MM-dd");
            obj = Calendar.DAY_OF_YEAR;
            flag = 9;
        } else if (type != null && type == 2) {
            format = new SimpleDateFormat("yyyy-MM");
            obj = Calendar.MONTH;
            flag = 11;
        } else {
            format = new SimpleDateFormat("yyyy");
            obj = Calendar.YEAR;
            flag = 9;
        }
        if (StringUtils.isNotEmpty(date)) {
            startDate = date.substring(0, 10);
            endDate = date.substring(date.length() - 10, date.length());
            try {
                begin = format.parse(startDate);
                end = format.parse(endDate);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        } else {
            end = new Date();
            begin = getDateBefore(end, flag, obj);
        }

        List<String> result = new ArrayList<String>();
        Calendar tempStart = Calendar.getInstance();
        tempStart.setTime(begin);
        while (begin.getTime() <= end.getTime()) {
            result.add(format.format(tempStart.getTime()));
            tempStart.add(obj, 1);
            begin = tempStart.getTime();
        }
        return result;
    }

    /**
     * 拆分时间间隔
     * Title: getDateArray
     * Description: '2018-04-04 - 2018-05-22'
     */
    public static String[] getDateArray(String date) {
        String[] defalut = {"", ""};
        try {
            if (StringUtils.isNotEmpty(date)) {
                String startDate = date.substring(0, 10);
                String endDate = date.substring(date.length() - 10, date.length());
                String[] dates = {startDate, endDate};
                return dates;
            } else {
                return defalut;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return defalut;
        }
    }

    public static Date getDateBefore(Date d, int day, Integer type) {
        Calendar now = Calendar.getInstance();
        now.setTime(d);
        now.set(type, now.get(type) - day);
        return now.getTime();
    }

    /**
     * 获取给定日期之前或之后的日期
     *
     * @param date
     * @param type
     * @param num
     * @return
     */
    public static String getPreviouslyDate(Date date, String type, Integer num) {

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String resultDate = "";
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        try {
            switch (type) {
                case "day":
                    c.add(Calendar.DATE, num);
                    break;
                case "month":
                    c.add(Calendar.MONTH, num);
                    break;
                case "year":
                    c.add(Calendar.YEAR, num);
                    break;
                default:
                    c.add(Calendar.DATE, 0);
                    break;
            }
            Date d = c.getTime();
            resultDate = format.format(d);
            return resultDate;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * ******************************************
     * 方法名称:判断日期是否是某一天
     *
     * @param date
     * @param num
     * @return
     * @return:boolean ******************************************
     */
    public static boolean isOneday(Date date, Integer num) {
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
        String dateStr = fmt.format(date).toString();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.DAY_OF_MONTH, num);
        String oneDateStr = fmt.format(calendar.getTime()).toString();
        if (dateStr.equals(oneDateStr)) {//格式化为相同格式
            return true;
        } else {
            return false;
        }
    }

    public static void main(String[] args) {
//      System.out.println(DateUtils.getPreviouslyDate(new Date(), "day", -1));
//      String[] dates = getDateArray("2018-04-04 - 2018-05-22");
//      for (int i = 0; i < dates.length; i++) {
//          System.out.println(dates[i].toString());
//      }
//      System.out.println(getBetweenDates("2018-04-11","2018-04-11",1));
//      System.out.println(DateUtils.getPreviouslyDate(new Date(), "day", 0));
//        System.out.println(DateUtils.getPreviouslyDate(new Date(),"month",-7));
        /*System.out.println(getBetweenSecond("2017-11-14 21:30:46","2017-11-14 21:33:46"));*/

        Date current = new Date();
        String dateStr = " 19:00:00";
        String currentDateStr = TimeUtils.parseDate(current, "yyyy-MM-dd") + dateStr;

        //获取当天时间差
        Integer betweenMinutes = getBetweenMinutes(currentDateStr, TimeUtils.parseDate(new Date()));
        System.out.println(betweenMinutes);
    }
}
